package com.example.demo.controller;


import com.example.demo.common.OnlyMath;
import com.example.demo.dao.ClassificationTableDao;
import com.example.demo.dao.CommodityListDao;
import com.example.demo.dao.PictureSheetOfMerchandiseDao;
import com.example.demo.dao.WarehouseDao;
import com.example.demo.pojo.*;
import com.example.demo.service.Position;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/appGoods")
@Api(value = "前台商品展示",tags = "前台商品展示")
@Controller
public class AppGoodShowController {
    @Autowired
    Position position;

    @Autowired
    ClassificationTableDao classificationTableDao;

    @Autowired
    CommodityListDao commodityListDao;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    PictureSheetOfMerchandiseDao pictureSheetOfMerchandiseDao;



    @RequestMapping(value = "/position",method = RequestMethod.GET)
    @ApiOperation(value = "通过地理位置获取相对应的仓库")
    @ApiImplicitParams({@ApiImplicitParam(value = "经度",name="longitude"),@ApiImplicitParam(value = "纬度",name="latitude")})
    public String findCoordinates(HttpServletRequest request,double longitude, double latitude){
        List<Warehouse> list=position.findAll();
        OnlyMath onlyMath=new OnlyMath();
        String message=String.valueOf(onlyMath.warehouse(list,longitude,latitude));
        request.getSession().setAttribute("warehouse",message);
        return "redirect:/appGoods/firstClassfiyGoods";
    }


    @ResponseBody
    @RequestMapping(value ="/firstClassfiyGoods",method = RequestMethod.GET)
    @ApiOperation(value = "首次获取首页商品的一级分类，和热卖的二级分类")
    @ApiImplicitParam(value = "一级分类id",name = "id" ,required = false)
    public Map findClassfiyGoods(HttpServletRequest request,Integer id){

        String warehouse= (String) request.getSession().getAttribute("warehouse");

        Map<String,List<ClassificationTable>> map=new HashMap<>();
        //查询一级分类
        List<ClassificationTable> list=position.allClassfiyByWarehouse(warehouse);
        map.put("firstClassify",list);

        if(id!=null){
            ClassificationTable classificationTable=new ClassificationTable();
            classificationTable.setPid(id);
            classificationTable.setCangku(warehouse);
            List<ClassificationTable> list1=classificationTableDao.Classficationtable1(classificationTable);
            for (int a=0;a<list1.size();a++){
                ClassificationTable classificationTable1=new ClassificationTable();
                classificationTable1.setClassificationid(list1.get(a).getClassificationid());
                classificationTable1.setCangku(warehouse);
                List<CommodityList> goodsList=commodityListDao.findGoodsByClassificationid(classificationTable1);
                for (int b=0;b<goodsList.size();b++){
                    PictureSheetOfMerchandise pictureSheetOfMerchandise=new PictureSheetOfMerchandise();
                    pictureSheetOfMerchandise.setGoodsid(goodsList.get(b).getGoodsid());
                    pictureSheetOfMerchandise.setIss(1);
                    goodsList.get(b).setPicturepath(pictureSheetOfMerchandiseDao.selectByGoodis(pictureSheetOfMerchandise).get(0).getPictureaddress());
                }
               list1.get(a).setCommodityLists(goodsList);
            }
            map.put("secondClassfiy",list1);
        }else {
            ClassificationTable classificationTable=new ClassificationTable();
            classificationTable.setPid(1);
            classificationTable.setCangku(warehouse);
            List<ClassificationTable> list1=classificationTableDao.Classficationtable1(classificationTable);
            for (int a=0;a<list1.size();a++){
                ClassificationTable classificationTable1=new ClassificationTable();
                classificationTable1.setClassificationid(list1.get(a).getClassificationid());
                classificationTable1.setCangku(warehouse);
                List<CommodityList> goodsList=commodityListDao.findGoodsByClassificationid(classificationTable1);
                System.out.println(goodsList.toString());
                for (int b=0;b<goodsList.size();b++){
                    PictureSheetOfMerchandise pictureSheetOfMerchandise=new PictureSheetOfMerchandise();
                    pictureSheetOfMerchandise.setGoodsid(goodsList.get(b).getGoodsid());
                    pictureSheetOfMerchandise.setIss(1);
                    goodsList.get(b).setPicturepath(pictureSheetOfMerchandiseDao.selectByGoodis(pictureSheetOfMerchandise).get(0).getPictureaddress());
                }
                list1.get(a).setCommodityLists(goodsList);
            }
            map.put("secondClassfiy",list1);
        }
     return map;
    }


    @ResponseBody
    @RequestMapping(value = "/secondClassfiyGoodsid",method = RequestMethod.GET)
    @ApiOperation(value = "第二次通过一级分类获取二级分类和商品信息")
    @ApiImplicitParam(value = "一级分类id",name = "id")
    public List findClassfiyGoodsSecond(HttpServletRequest request,int id){
        String warehouse= (String) request.getSession().getAttribute("warehouse");
        ClassificationTable classificationTable=new ClassificationTable();
        classificationTable.setPid(id);
        classificationTable.setCangku(warehouse);
        List<ClassificationTable> list1=classificationTableDao.Classficationtable1(classificationTable);
        for (int a=0;a<list1.size();a++){
            ClassificationTable classificationTable1=new ClassificationTable();
            classificationTable1.setClassificationid(list1.get(a).getClassificationid());
            classificationTable1.setCangku(warehouse);
            List<CommodityList> goodsList=commodityListDao.findGoodsByClassificationid(classificationTable1);
            System.out.println(goodsList.toString());
            for (int b=0;b<goodsList.size();b++){
                PictureSheetOfMerchandise pictureSheetOfMerchandise=new PictureSheetOfMerchandise();
                pictureSheetOfMerchandise.setGoodsid(goodsList.get(b).getGoodsid());
                pictureSheetOfMerchandise.setIss(1);
                goodsList.get(b).setPicturepath(pictureSheetOfMerchandiseDao.selectByGoodis(pictureSheetOfMerchandise).get(0).getPictureaddress());
            }
            list1.get(a).setCommodityLists(goodsList);
        }
        return list1;
    }

    @ResponseBody
    @RequestMapping(value = "/findGoodsRedies",method = RequestMethod.GET)
    @ApiOperation(value = "通过商品id查找redies里边的具体商品信息")
    @ApiImplicitParam(value = "商品",name = "goodsid")
    public GoodsMessage findGoodsReadies(int goodsid){
        GoodsMessage goodsMessage=new GoodsMessage();
        String message="goodsid"+goodsid;
        Map a=redisTemplate.opsForHash().entries(message);
        String b=(String) a.get("first");
        Gson gson=new Gson();
        goodsMessage.setCommodityList(gson.fromJson(b,CommodityList.class));
        String list= (String) a.get("second");
        goodsMessage.setFirst(gson.fromJson(list,new TypeToken<List<String>>(){}.getType()));
        String list1= (String) a.get("third");
        goodsMessage.setSecond(gson.fromJson(list1,new TypeToken<List<String>>(){}.getType()));
        return goodsMessage;
    }
}
